package Control;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.*;
import java.util.Date;
import Database.*;
import Model.*;

import javax.imageio.ImageIO;
import javax.swing.*;

public class AddSupplier extends JFrame implements ActionListener
{
	private JLabel name, logoPicture;
	private JTextField jtName;
	private JButton add, cancel;
	private JPanel logo, topPanel, bottomPanel;
	private TableOperations to;
	private ImageIcon icon = new ImageIcon("frameIcon.png");

	public AddSupplier()
	{
		setIconImage(icon.getImage());
		setTitle("MDL - Add Supplier");
		setSize(350, 300);
		setLocationRelativeTo(null);
		setResizable(false);
		setLayout(new GridLayout(3, 1));
		to = new TableOperations();

		GridBagLayout gridbagLayoutSettings = new GridBagLayout();
		GridBagConstraints gc = new GridBagConstraints();
		Insets insets = new Insets(5, 5, 5, 5);

		BufferedImage myPicture = null;
		try
		{
			myPicture = ImageIO.read(new File("logo.png"));
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		logo = new JPanel();
		logoPicture = new JLabel(new ImageIcon(myPicture));
		logo.add(logoPicture);
		this.add(logo);

		// Top Panel
		topPanel = new JPanel();
		topPanel.setLayout(gridbagLayoutSettings);

		name = new JLabel("Name");
		gc.gridx = 0;
		gc.gridy = 1;
		gc.weighty = 0;
		gc.weightx = 0;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		topPanel.add(name, gc);

		jtName = new JTextField(25);
		gc.gridx = 1;
		gc.gridy = 1;
		gc.weighty = 0;
		gc.weightx = 2;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		topPanel.add(jtName, gc);

		this.add(topPanel, BorderLayout.CENTER);

		// BottomPanel
		bottomPanel = new JPanel();
		bottomPanel.setLayout(gridbagLayoutSettings);

		gc.gridx = 1;
		gc.gridy = 1;
		gc.weighty = 0;
		gc.weightx = 0;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		add = new JButton("Add");
		add.addActionListener(this);
		bottomPanel.add(add, gc);

		gc.gridx = 3;
		gc.gridy = 1;
		gc.weighty = 0;
		gc.weightx = 0;
		gc.insets = insets;
		gc.fill = GridBagConstraints.BOTH;
		cancel = new JButton("Cancel");
		cancel.addActionListener(this);
		bottomPanel.add(cancel, gc);

		this.add(bottomPanel, BorderLayout.SOUTH);

	}

	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() == cancel)
			this.dispose();
		else if (e.getSource() == add)
		{
			if (jtName.getText().equals(""))
				JOptionPane.showMessageDialog(null, "Check for blank fields");
			else
			{
				Supplier s = new Supplier(jtName.getText());
				to.addSupplier(s);
				jtName.setText("");
			}
		}
	}
}